package com.lgh.demo.viewModel;

import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.lgh.demo.MyApplication;
import com.lgh.demo.R;
import com.lgh.demo.activity.DetailsActivity;
import com.lgh.demo.activity.SellersAcitivity;
import com.lgh.demo.databinding.ActivitySellersBinding;
import com.lgh.demo.util.ActivityUtils;
import com.lgh.demo.util.DataUtils;
import com.lgh.demo.util.ViewUtil;
import com.lgh.demo.util.WMConstantUtil;
import com.lgh.demo.view.BorderView;
import com.sankuai.waimai.opensdk.MTWMApi;
import com.sankuai.waimai.opensdk.callback.LoadDataListener;
import com.sankuai.waimai.opensdk.response.model.PoiBaseInfo;
import com.sankuai.waimai.opensdk.response.model.PoiListData;

public class VMSellers {


    private int pageIndex = 0;
    private int pageSize = 4;
    private boolean needShowFirst = false;
    private PoiListData poiListData;

    private SellersAcitivity sellersAcitivity;
    private ActivitySellersBinding sellersBinding;
    private BorderView borderView;

    public VMSellers(SellersAcitivity sellersAcitivity, ActivitySellersBinding sellersBinding) {
        this.sellersAcitivity = sellersAcitivity;
        this.sellersBinding = sellersBinding;
        loadData();

        sellersBinding.itemSellers1.llContent.setOnKeyListener(new PreListener());
        sellersBinding.itemSellers3.llContent.setOnKeyListener(new PreListener());
        sellersBinding.itemSellers2.llContent.setOnKeyListener(new NextListener());
        sellersBinding.itemSellers4.llContent.setOnKeyListener(new NextListener());
        initBorderView();
    }

    class NextListener implements View.OnKeyListener{
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && event.getAction() == KeyEvent.ACTION_DOWN) {
                loadNextData();
                return true;
            }
            return false;
        }
    }

    class PreListener implements View.OnKeyListener{
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && event.getAction() == KeyEvent.ACTION_DOWN) {
                loadPreData();
                return true;
            }
            return false;
        }
    }

    private void initBorderView(){
        borderView = new BorderView(sellersAcitivity);
        borderView.setBackgroundResource(R.drawable.focus_default);
        borderView.attachTo(sellersBinding.flContent);
    }

    public boolean loadNextData() {
        pageIndex++;
        if (poiListData != null && DataUtils.getTotalPage(poiListData.getPoiTotalNum(), pageSize) < pageIndex) {
            pageIndex--;
            return false;
        }
        needShowFirst = true;
        loadData();
        return true;
    }

    public boolean loadPreData() {
        pageIndex--;
        if (pageIndex < 1) {
            pageIndex = 1;
            return false;
        }
        needShowFirst = true;
        loadData();
        return true;
    }

    private void loadData() {
        MTWMApi.getPoiList(MyApplication.longtitude, MyApplication.latitude, "", 0, pageIndex, pageSize,
                new LoadDataListener<PoiListData>() {

                    @Override
                    public void onSuccess(PoiListData data) {
                        poiListData = data;
                        if (needShowFirst) {
                            sellersBinding.itemSellers1.llContent.requestFocus();
                        }
                        Log.d("getPoiList", data.toString());
                        DataUtils.addNullToListData(data.getPoiInfoList(), pageSize);
                        sellersBinding.setPoiListData(data);
                        sellersBinding.setPageInfo(data.getCurrentPageIndex() + "/"
                                + DataUtils.getTotalPage(poiListData.getPoiTotalNum(), pageSize));
                    }

                    @Override
                    public void onError(String errorMsg) {
                        Toast.makeText(sellersAcitivity, "获取数据失败，" + errorMsg, Toast.LENGTH_SHORT).show();
                    }

                });
    }

    public void turnToSellerDetail(View view) {
        int index = ViewUtil.getChildViewIndex(sellersBinding.flContent, view);
        PoiBaseInfo poiBaseInfo = poiListData.getPoiInfoList().get(index);
        Intent intent = new Intent(sellersAcitivity, DetailsActivity.class);
        intent.putExtra(WMConstantUtil.KEY_POI_ID, poiBaseInfo.getWmPoiId());
        intent.putExtra(WMConstantUtil.KEY_LATITUDE, poiBaseInfo.getLatitude());
        intent.putExtra(WMConstantUtil.KEY_LONGITUDE, poiBaseInfo.getLongitude());
        ActivityUtils.gotoActivityWithIntent(sellersAcitivity, intent);
    }

}
